package com.heu.blood.transfusion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import com.heu.blood.transfusion.dao.TransfusionAgreementDao;
import com.heu.blood.transfusion.entity.TransfusionAgreementEntity;
import com.heu.blood.transfusion.service.TransfusionAgreementService;
import com.heu.blood.transfusion.vo.TransfusionAgreementVo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.heu.blood.common.enums.CommonEnum.IF_SHOW;


@Service("transfusionAgreementService")
public class TransfusionAgreementServiceImpl extends ServiceImpl<TransfusionAgreementDao, TransfusionAgreementEntity> implements TransfusionAgreementService {
    @Autowired
    private TransfusionAgreementDao transfusionAgreementDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 构造TransfusionAgreementEntity对象
        TransfusionAgreementEntity transfusionAgreement = new TransfusionAgreementEntity(); // 用于接收传递过来的TransfusionAgreementEntity参数
        if (Strings.isNotBlank((String) params.get("transfusionId"))){
            transfusionAgreement.setTransfusionId((String) params.get("transfusionId")); // 添加transfusionId
        }
        if (Strings.isNotBlank((String) params.get("patientId"))){
            transfusionAgreement.setPatientId((String) params.get("patientId")); // 添加patientId
        }
        if (Strings.isNotBlank((String) params.get("name"))){
            transfusionAgreement.setName((String) params.get("name")); // 添加name
        }
        if (Strings.isNotBlank((String) params.get("finalSuggestion"))){
            transfusionAgreement.setFinalSuggestion((String) params.get("finalSuggestion")); // 添加finalSuggestion
        }

        // 构造条件构造器对象
        LambdaQueryWrapper<TransfusionAgreementEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.eq(Strings.isNotBlank(transfusionAgreement.getTransfusionId()), TransfusionAgreementEntity::getTransfusionId, transfusionAgreement.getTransfusionId())
                .eq(Strings.isNotBlank(transfusionAgreement.getPatientId()), TransfusionAgreementEntity::getPatientId, transfusionAgreement.getPatientId())
                .eq(Strings.isNotBlank(transfusionAgreement.getName()), TransfusionAgreementEntity::getName, transfusionAgreement.getName())
                .eq(Strings.isNotBlank(String.valueOf(transfusionAgreement.getIfShow())), TransfusionAgreementEntity::getIfShow,IF_SHOW.code())
                .eq(Strings.isNotBlank(transfusionAgreement.getFinalSuggestion()), TransfusionAgreementEntity::getFinalSuggestion, transfusionAgreement.getFinalSuggestion());

        // 根据输血申请单号升序排列
        queryWrapper.orderByAsc(TransfusionAgreementEntity::getTransfusionId);
        IPage<TransfusionAgreementEntity> page = this.page(
                new Query<TransfusionAgreementEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public TransfusionAgreementEntity queryByPatientId(String patientId) {

        // 构造queryWrapper对象
        LambdaQueryWrapper<TransfusionAgreementEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.eq(Strings.isNotBlank(patientId), TransfusionAgreementEntity::getPatientId, patientId);
        return transfusionAgreementDao.selectOne(queryWrapper);

    }

    @Override
    public TransfusionAgreementEntity queryByTransfusionId(String transfusionId) {
        LambdaQueryWrapper<TransfusionAgreementEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(transfusionId), TransfusionAgreementEntity::getTransfusionId,transfusionId);
        return  transfusionAgreementDao.selectOne(queryWrapper);
    }

    /**
     * 获取输血申请同意书的打印预览内容
     * @param transfusionId
     * @return
     */
    @Override
    public TransfusionAgreementVo getAgreement(String transfusionId) {
        TransfusionAgreementVo transfusionAgreementVo = transfusionAgreementDao.Agreement(transfusionId);
        System.out.println(transfusionAgreementVo);
        return transfusionAgreementVo;
    }

}